/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calafie.builder.ihm.generated;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.DefaultCaret;

import calafie.builder.Builder;
import calafie.builder.Util;
import calafie.builder.ihm.controleur.DefaultNumericListener;
import calafie.builder.ihm.modele.swing.FloatDocument;
import calafie.builder.ihm.modele.swing.OrdreNiveau;
import calafie.builder.ihm.modele.type.CaractEnum;
import calafie.builder.ihm.modele.type.ComptEnum;
import calafie.builder.ihm.modele.type.Niveau;
import calafie.builder.ihm.modele.type.TypeLegalite;
import calafie.builder.ihm.modele.type.TypeOrdre;
import calafie.builder.jaxb.Ordre;

/**
 * 
 * @author Dart
 */
public class PopUpOrdre extends JDialog {

    /**
     * 
     */
    private static final long serialVersionUID = -4861014700346408863L;

    private boolean save;

    /**
     * Creates new form PopUpOrdreG
     */
    public PopUpOrdre() {
        super(Builder.getInstance().getFenetre(), true);
        initComponents();
        setSize(530, 400);
        setResizable(false);
        setLocationRelativeTo(Builder.getInstance().getFenetre());

        typeLegalCombo.setModel(new javax.swing.DefaultComboBoxModel(
                TypeLegalite.getModele()));

        String[] caractOppo = new String[CaractEnum.getModele().length + 1];
        caractOppo[0] = "";
        for (int i = 1; i <= CaractEnum.getModele().length; i++) {
            caractOppo[i] = CaractEnum.getModele()[i - 1];
        }

        caractOppoCombo.setModel(new javax.swing.DefaultComboBoxModel(
                caractOppo));

        competeCombo.setModel(new javax.swing.DefaultComboBoxModel(ComptEnum
                .getModele()));

        caractCombo.setModel(new javax.swing.DefaultComboBoxModel(CaractEnum
                .getModele()));
        typeCombo.setModel(new javax.swing.DefaultComboBoxModel(TypeOrdre
                .getModele()));
        comboNiveau.setModel(new javax.swing.DefaultComboBoxModel(
                Niveau.getModele()));
        
        descriptionArea.setLineWrap(true);        
        descriptionArea.setWrapStyleWord(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        JLabel nomLabel = new javax.swing.JLabel();
        nomFiled = new javax.swing.JTextField();
        JLabel typeLabel = new javax.swing.JLabel();
        typeCombo = new javax.swing.JComboBox();
        JLabel descriptionLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionArea = new javax.swing.JTextArea();
        legalCheck = new javax.swing.JCheckBox();
        politiqueChech = new javax.swing.JCheckBox();
        typeLegalCombo = new javax.swing.JComboBox();
        JLabel typeLegalLabel = new javax.swing.JLabel();
        automatiqueCheck = new javax.swing.JCheckBox();
        JLabel caractLabel = new javax.swing.JLabel();
        caractCombo = new javax.swing.JComboBox();
        JLabel competeLabel = new javax.swing.JLabel();
        competeCombo = new javax.swing.JComboBox();
        JLabel diffLabel = new javax.swing.JLabel();
        diffField = new javax.swing.JTextField();
        JLabel caractOppoLabel = new javax.swing.JLabel();
        caractOppoCombo = new javax.swing.JComboBox();
        coutPvField = new javax.swing.JTextField(); 
        JLabel coutPvLabel = new javax.swing.JLabel();
        JLabel coutArgentLabel = new javax.swing.JLabel();
        coutArgentField = new javax.swing.JTextField();


        diffField.addKeyListener(new DefaultNumericListener());
        coutArgentField.addKeyListener(new DefaultNumericListener());
        coutPvField.setDocument(new FloatDocument());

        nomLabel.setText(Util.getMessage(Util
                .getMessage("builder.popOrdre.element.nom")));
        typeLabel.setText(Util.getMessage("builder.popOrdre.element.type"));
        descriptionLabel.setText(Util
                .getMessage("builder.popOrdre.element.desc"));
        typeLegalLabel.setText(Util
                .getMessage("builder.popOrdre.element.gravite"));
        caractLabel.setText(Util.getMessage("builder.popOrdre.element.caract"));
        competeLabel.setText(Util
                .getMessage("builder.popOrdre.element.competence"));
        diffLabel.setText(Util.getMessage("builder.popOrdre.element.dif"));
        caractOppoLabel.setText(Util
                .getMessage("builder.popOrdre.element.caractoppo"));
        coutArgentLabel.setText(Util
                .getMessage("builder.popOrdre.element.cout"));
        coutPvLabel.setText(Util.getMessage("builder.popOrdre.element.coutpv"));

        automatiqueCheck.setText(Util
                .getMessage("builder.popOrdre.element.automatique"));
        legalCheck.setText(Util.getMessage("builder.popOrdre.element.legal"));
        politiqueChech.setText(Util
                .getMessage("builder.popOrdre.element.politique"));

        okButton.setText(Util.getMessage("builder.button.save"));

        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                clicSave();
            }
        });

        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                clicCancel();
            }
        });

        automatiqueCheck.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                clicAutomatique();
            }
        });

        cancelButton.setText(Util.getMessage("builder.button.cancel"));

        legalCheck.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                clicLegal();
            }
        });

        
        diffField.addFocusListener(new FocusListener() {
            
            public void focusLost(FocusEvent e) {
                
            }
            
            public void focusGained(FocusEvent e) {
                diffField.selectAll();
                
            }
        });
        
        
        coutArgentField.addFocusListener(new FocusListener() {
            
            public void focusLost(FocusEvent e) {
                
            }
            
            public void focusGained(FocusEvent e) {
                coutArgentField.selectAll();
                
            }
        });
        
        
        coutPvField.addFocusListener(new FocusListener() {
            
            public void focusLost(FocusEvent e) {
                
            }
            
            public void focusGained(FocusEvent e) {
                coutPvField.selectAll();
                
            }
        });
        
        

        jScrollPane1.setViewportView(descriptionArea);

        JPanel panel = new JPanel();
        this.add(panel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(0, 0, Short.MAX_VALUE)
                                    .addComponent(okButton)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(cancelButton))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(nomLabel)
                                            .addGap(18, 18, 18)
                                            .addComponent(nomFiled, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(typeLabel)
                                            .addGap(18, 18, 18)
                                            .addComponent(typeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(comboNiveau, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(descriptionLabel))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(legalCheck, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(politiqueChech))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(typeLegalLabel)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(typeLegalCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGap(0, 100, Short.MAX_VALUE)))
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(automatiqueCheck)
                                    .addGap(18, 18, 18)
                                    .addComponent(diffLabel)
                                    .addGap(18, 18, 18)
                                    .addComponent(diffField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(caractLabel)
                                        .addComponent(competeLabel))
                                    .addGap(28, 28, 28)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(caractCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(101, 101, 101)
                                            .addComponent(caractOppoLabel)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(caractOppoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(competeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(coutPvLabel)
                                    .addGap(27, 27, 27)
                                    .addComponent(coutPvField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(coutArgentLabel)
                                    .addGap(18, 18, 18)
                                    .addComponent(coutArgentField, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane1))
                            .addGap(0, 0, Short.MAX_VALUE))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(nomLabel)
                        .addComponent(nomFiled, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(legalCheck))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(typeLabel)
                        .addComponent(typeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboNiveau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(politiqueChech))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(descriptionLabel)
                        .addComponent(typeLegalLabel)
                        .addComponent(typeLegalCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(9, 9, 9)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(automatiqueCheck)
                        .addComponent(diffLabel)
                        .addComponent(diffField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(caractOppoLabel)
                            .addComponent(caractOppoCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(caractLabel)
                            .addComponent(caractCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(competeLabel)
                        .addComponent(competeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(coutPvField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(coutPvLabel)
                        .addComponent(coutArgentLabel)
                        .addComponent(coutArgentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(okButton)
                        .addComponent(cancelButton))
                    .addContainerGap())
            );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox automatiqueCheck;
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox caractCombo;
    private javax.swing.JComboBox caractOppoCombo;
    private javax.swing.JComboBox competeCombo;
    private javax.swing.JTextField coutPvField;
    private javax.swing.JTextArea descriptionArea;
    private javax.swing.JTextField diffField;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField coutArgentField;
    private javax.swing.JCheckBox legalCheck;
    private javax.swing.JTextField nomFiled;
    private javax.swing.JButton okButton;
    private javax.swing.JCheckBox politiqueChech;
    private javax.swing.JComboBox typeCombo;
    private javax.swing.JComboBox typeLegalCombo;

    // End of variables declaration//GEN-END:variables
    private Ordre ordre;
    private OrdreNiveau ordreNiveau;
    
    private JComboBox comboNiveau = new JComboBox();
    

    public Ordre ouverture(Ordre ordre) {
        
        chargerOrdre(ordre);
        comboNiveau.setVisible(false);
        // Bloquant, la suite du traitement n'est effectué uniquement lorsque la popup n'est plus affichée.
        setVisible(true);
        // Si on a cliqué sur annuler on renvoi null
        // sinon on remplit l'ordre et on renvoi l'ordre
        if (!save) {
            return null;
        }

        remplirOrdre();

        return this.ordre;
    }

    
    private void remplirOrdre() {
        ordre.setNom(nomFiled.getText().trim());
        ordre.setAutomatique(automatiqueCheck.isSelected());
        ordre.setLegal(!legalCheck.isSelected());
        ordre.setDescription(descriptionArea.getText().trim());

        ordre.setType(typeCombo.getSelectedItem().toString());

        if (!ordre.isLegal()) {
            ordre.setPolitique(politiqueChech.isSelected());
            ordre.setGravite(TypeLegalite.getVal(typeLegalCombo.getSelectedIndex()).name());
        }

        if (!ordre.isAutomatique()) {
            ordre.setCompetence(competeCombo.getSelectedItem().toString());
            ordre.setCaracteristique(caractCombo.getSelectedItem().toString());
            ordre.setOposition(caractOppoCombo.getSelectedItem().toString());


            String diff = diffField.getText();


            if (diff.length() > 0) {
                ordre.setDifficulte(Integer.parseInt(diff));
            }

        }
        
        
        String coutPv = coutPvField.getText();
        String cout = coutArgentField.getText();
        if (coutPv.length() > 0) {
            ordre.setPv(Float.parseFloat(coutPv));
        }

        if (cout.length() > 0) {
            ordre.setArgent(Double.parseDouble(cout));
        }


    }

    private void chargerOrdre (Ordre ordreParam) {
        
        if (ordreParam == null) {
            ordre = new Ordre();
            coutPvField.setText("0");
            coutArgentField.setText("0");
            diffField.setText("0");
            nomFiled.setText("");
            descriptionArea.setText("");
            typeCombo.setSelectedIndex(0);
            
            legalCheck.setSelected(false);
            typeLegalCombo.setSelectedIndex(0);
            automatiqueCheck.setSelected(false);
           
            competeCombo.setSelectedIndex(0);
            caractCombo.setSelectedIndex(0);
            caractOppoCombo.setSelectedIndex(0);
            
        } else {
            ordre = ordreParam;
            nomFiled.setText(ordreParam.getNom());
            
            DefaultCaret caret = (DefaultCaret) descriptionArea.getCaret();
            caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
            descriptionArea.setText(ordreParam.getDescription());
            caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
            
            typeCombo.setSelectedItem(ordreParam.getType());
            
            legalCheck.setSelected(!ordreParam.isLegal());
            automatiqueCheck.setSelected(ordreParam.isAutomatique());
            
            if (legalCheck.isSelected()) {
                politiqueChech.setSelected(ordreParam.isPolitique());
                typeLegalCombo.setSelectedItem(TypeLegalite.valueOf(ordreParam.getGravite()).getNom());
            }
            
            if (!automatiqueCheck.isSelected()) {
                competeCombo.setSelectedItem(ordreParam.getCompetence());
                caractCombo.setSelectedItem(ordreParam.getCaracteristique());
                caractOppoCombo.setSelectedItem(ordreParam.getOposition());
                diffField.setText(ordreParam.getDifficulte().toString());
            }
            coutArgentField.setText(""+ordreParam.getArgent());
            coutPvField.setText(""+ordreParam.getPv());
        }
        // Initialisation de la partie légal et jet
        clicLegal();
        clicAutomatique();

    }
    
    
    public OrdreNiveau ouvertureNiveau(OrdreNiveau ordre) {
        
        comboNiveau.setVisible(true);
        if (ordre == null) {
            chargerOrdre(null);
            ordreNiveau = new OrdreNiveau(Niveau.NIVEAU_UN, this.ordre);
            comboNiveau.setSelectedIndex(0);
        } else {
            ordreNiveau = ordre;
            chargerOrdre(ordre.getOrdre());
            comboNiveau.setSelectedItem(ordre.getNiveau().toString());
        }
        
        
        // Bloquant, la suite du traitement n'est effectué uniquement lorsque la popup n'est plus affichée.
        setVisible(true);
        // Si on a cliqué sur annuler on renvoi null
        // sinon on remplit l'ordre et on renvoi l'ordre
        if (!save) {
            return null;
        }

        remplirOrdre();
        
        Niveau niv = Niveau.getNiveau(comboNiveau.getSelectedItem().toString());
        ordreNiveau.setNiveau(niv);
        
        
        return ordreNiveau;
    }
    
    
    
    private void clicSave() {
        save = true;
        setVisible(false);
    }

    private void clicCancel() {
        save = false;
        setVisible(false);
    }

    private void clicAutomatique() {
        boolean enable = !automatiqueCheck.isSelected();
        caractCombo.setEnabled(enable);
        caractOppoCombo.setEnabled(enable);
        competeCombo.setEnabled(enable);
        diffField.setEnabled(enable);
    }

    private void clicLegal() {
        boolean enable = legalCheck.isSelected();
        typeLegalCombo.setEnabled(enable);
        politiqueChech.setEnabled(enable);
    }

}
